//
//  MineRegisterMailVC.m
//  AroundTheWorld
//
//  Created by dllo on 16/5/30.
//  Copyright © 2016年 IfelseGo an. All rights reserved.
//

#import "MineRegisterMailVC.h"
#import "DataBaseHandle.h"


//枚举的定义
/*
typedef enum {
    //以下是枚举成员 TestA = 0,
    TestB,
    TestC,
    TestD
}Test;//枚举名称

枚举成员(枚举数据)是某类数据可能取值的集合.
 
    *1* 如一周内星期可能取值的集合为：{ Sun,Mon,Tue,Wed,Thu,Fri,Sat}。
    *2* 数据枚举的顺序规定了枚举数据的序号，从0开始，依次递增。
 
 
*/


//注册成功与否的情况集合
typedef enum { Valid, MailNameEmpty, UserNameEmpty, UserNameRepeat, InvalidPasswordEmpty, InvalidPasswordError} LoginResult;

@interface NSString (Validation)

+ (BOOL)isEmpty:(NSString *)string;

@end

@implementation NSString (Validation)

+ (BOOL)isEmpty:(NSString *)string;
{
    if (string == nil) {
        return YES;
    }
    
    if (string.length == 0) {
        return YES;
    }
    
    return NO;
}

@end


@interface MineRegisterMailVC ()

@end

@implementation MineRegisterMailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.passTextF.secureTextEntry = YES;
//    self.view.backgroundColor = [UIColor colorWithRed:76 / 255.0 green:197 / 255.0 blue:143 / 255.0 alpha:1.0];
    //返回按钮
    [self.cancleButton addTarget:self action:@selector(cancleButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    //注册完成按钮
    [self.finishButton addTarget:self action:@selector(finishButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    // Do any additional setup after loading the view from its nib.
}
//返回按钮
- (void)cancleButtonAction:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}
//注册完成
- (void)finishButtonAction:(UIButton *)button
{
    //点击注册按钮后
    LoginResult result = [self canLogin];
    if (result != Valid) {
        switch (result) {
            case MailNameEmpty:
                [self addAlertControllerWithTitle:@"注册失败" withMessage:@"邮箱不能为空!"];
                break;
            case UserNameEmpty:
                [self addAlertControllerWithTitle:@"注册失败" withMessage:@"用户名不能为空!"];
                break;
            case UserNameRepeat:
                [self addAlertControllerWithTitle:@"注册失败" withMessage:@"用户名已存在!"];
                break;
            case InvalidPasswordEmpty:
                [self addAlertControllerWithTitle:@"注册失败" withMessage:@"密码不能为空!"];
                break;
            case InvalidPasswordError:
                [self addAlertControllerWithTitle:@"注册失败" withMessage:@"密码位数不正确"];
                break;
            default:
                break;
        }
        return;
    }
    [self login];

}
//用户名空时返回
- (LoginResult)canLogin {
    //  user name
    NSString *maliTextF = self.mailTextV.text;
    if ([NSString isEmpty:maliTextF]) {
        return MailNameEmpty;
    }
    //  user name
    NSString *idTextF = self.IDtextF.text;
    if ([NSString isEmpty:idTextF]) {
        return UserNameEmpty;
    }
    //用户名重复确认
    NSString *userName = self.IDtextF.text;
    NSLog(@"%@", userName);
    NSMutableArray *array = [[DataBaseHandle shareDataBaseHandle] selectAllWithSQname:@"users" withCompare:YES];
    NSLog(@"%@",array);
    if ([array containsObject:self.IDtextF.text]) {
        return UserNameRepeat;
    }
    //密码确认
    NSString *pass = self.passTextF.text;
    if ([NSString isEmpty:pass]) {
        return InvalidPasswordEmpty;
    }
      //2次密码是否相等
//    if (![mm isEqualToString:qrmm]) {
//        return InvalidPasswordError;
//    }
    return Valid;
}


- (void)login {
    
    //保存数据到 用户名和密码字典里
    self.reModel = [[MineRegidterModel alloc] init];
    self.reModel.userID = self.IDtextF.text;
    self.reModel.password = self.passTextF.text;
    self.reModel.pic = @"http://tva2.sinaimg.cn/crop.0.0.180.180.180/5e300a5ajw1e8qgp5bmzyj2050050aa8.jpg";
    NSLog(@"%@",self.reModel);
    NSLog(@"self.IDtextF.text = %@", self.IDtextF.text);
    
    [[DataBaseHandle shareDataBaseHandle] insertMovie:self.reModel withSQname:@"users"];
    [self addValid];
}
// Error  AlertController
-(void)addAlertControllerWithTitle:(NSString *)acTitle withMessage:(NSString *)message
{
    UIAlertController * alertC = [UIAlertController alertControllerWithTitle:acTitle message:message preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel  handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertC addAction:cancelAction];
    //运行
    [self presentViewController:alertC animated:YES completion:^{
        
    }];
}
//
-(void)addValid{
    //创建
    UIAlertController * alertC = [UIAlertController alertControllerWithTitle:@"注册成功" message:@"返回登录画面!" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *defaltAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self dismissViewControllerAnimated:YES completion:^{
            
        }];
        
    }];
    [alertC addAction:defaltAction];
    [self presentViewController:alertC animated:YES completion:^{
        
    }];
}

//点击空白处收回键盘
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.mailTextV resignFirstResponder];
    [self.IDtextF resignFirstResponder];
    [self.passTextF resignFirstResponder];
    [self.yqmTextF resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
